package com.sanhuo.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YunPianUtil {
    //发送短信的api地址
    private static String appId
            = "de264f2aba987c3808093e7410ee7628";
    //短信模板
    private static String text = "【张梓燊测试】你有新的任务分配,请尽快查看";

    //模板发送接口的http地址
    private static String URI_TPL_SEND_SMS =
            "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
    //编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";

    //智能匹配模板发送接口的http地址
    private static String URI_SEND_SMS =
            "https://sms.yunpian.com/v2/sms/single_send.json";

    //发送短信的方法
    public static String sendSms(String mobile) {
        Map<String, String> params = new HashMap<>();
        params.put("apikey", appId);//使用短信授权的key
        params.put("text", text);//发送的内容,为短信网审核过的模板
        params.put("mobile", mobile);//发送到那个手机
        return post(URI_SEND_SMS, params);//调用post方法
    }

    public static String post(String url, Map<String, String> paramsMap) {
        //httpClient对象,通过该对象可以用java代码发起http请求
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            //url为发送短信的api地址,以此url为action的post请求
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                //paramMap存放了云片网授权的appId,收信人的手机和授信内容
                //将map转成一个list,这个list存放着post的请求内容
                List<NameValuePair> paramList = new ArrayList<>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(),
                            param.getValue());
                    paramList.add(pair);
                }
                //将发送的内容放到post的请求体内
                method.setEntity(new UrlEncodedFormEntity(paramList,
                        ENCODING));
            }
            //execute函数执行后,返回一个response,是http请求发送后的响应,即结果
            response = client.execute(method);
            //获取响应内容
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                //响应不为空的时候,以utf8的编码获取响应体的内容
                responseText = EntityUtils.toString(entity, ENCODING);
            }
            //出现异常则抛出异常
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭response对象,减少内存消耗
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //返回响应体的内容
        return responseText;
    }
}


